using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime;
using System.Resources;

namespace SSIS.Tasks
{
    public partial class SSISServerBrowser : Form
    {
        private string pkgPath;
        public string packagePath
        {
            get
            {
                return pkgPath;
            }
        }

        public SSISServerBrowser()
        {    
            InitializeComponent();
        }

        public SSISServerBrowser(string connection)
        {
            InitializeComponent();
            pkgPath = connection;

            string serverName = pkgPath.Substring(0, pkgPath.IndexOf("\\"));
            txtServerName.Text = serverName;

            string packageName =  pkgPath;//.Substring(serverName.Length + 1);

            btnConnect_Click(this, null);

            treeView1.SelectedNode = treeView1.Nodes.Find(packageName,true)[0];
        }


        private void SSISServerBrowser_Load(object sender, EventArgs e)
        {

            Icon serverIcon = (Icon)Properties.Resources.ServerIcon;
            Icon pkgIcon = (Icon)Properties.Resources.services;
            Icon folderIcon = (Icon)Properties.Resources.Folder;

            treeView1.ImageList = new ImageList();
            treeView1.ImageList.Images.Add(serverIcon);
            treeView1.ImageList.Images.Add(pkgIcon);
            treeView1.ImageList.Images.Add(folderIcon);
        }

        private void AddPackagesToTree(PackageInfos pkgInfos, TreeNode node, Microsoft.SqlServer.Dts.Runtime.Application app)
        {
            foreach (PackageInfo pkgInfo in pkgInfos)
            {
                if (pkgInfo.VersionGUID != null)
                { //Add Package
                    node.Nodes.Add(node.FullPath + "\\" + pkgInfo.Name,pkgInfo.Name, 1 ,1);
                }
                else //Add Folder
                {
                    TreeNode newRoot = node.Nodes.Add(node.FullPath + "\\" + pkgInfo.Name, pkgInfo.Name, 2, 2);

                    if ((node.FullPath + "\\" + pkgInfo.Name) == packagePath)
                    {
                        treeView1.SelectedNode = newRoot;
                    }

                    string ssisPath = newRoot.FullPath;
                    ssisPath = ssisPath.Replace(txtServerName.Text, "/");
                    ssisPath = ssisPath.Replace("\\","/");
                    PackageInfos newPkgInfos = app.GetPackageInfos(ssisPath, txtServerName.Text, null, null);
                    AddPackagesToTree(newPkgInfos, newRoot, app);
                }
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (txtServerName.Text!="")
            {
                Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
                PackageInfos pkgInfos = app.GetPackageInfos("/", txtServerName.Text, null, null);
                treeView1.Nodes.Clear();
                TreeNode root = treeView1.Nodes.Add(txtServerName.Text, txtServerName.Text, 0, 0);
                AddPackagesToTree(pkgInfos, root, app);
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            pkgPath = treeView1.SelectedNode.FullPath;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.ImageIndex == 1)
            {
                pkgPathTxt.Text = treeView1.SelectedNode.FullPath;
                okButton.Enabled = true;
            }
            else
            {
                pkgPathTxt.Text = "";
                okButton.Enabled = false;
            }
        }

        
    }
}